package cn.zyq.ykb.controller.h5.cps;



import cn.zyq.ykb.beans.cps.Cps;
import cn.zyq.ykb.beans.cps.GetByIDVo;
import cn.zyq.ykb.mapper.cps.CpsMapper;
import cn.zyq.ykb.service.cps.CpsService;
import cn.zyq.ykb.system.common.BaseController;
import cn.zyq.ykb.system.common.PageParam;
import com.alibaba.fastjson.JSONObject;
import com.taobao.api.ApiException;
import com.taobao.api.DefaultTaobaoClient;
import com.taobao.api.TaobaoClient;
import com.taobao.api.request.TbkDgMaterialOptionalUpgradeRequest;
import com.taobao.api.request.TbkDgMaterialRecommendRequest;
import com.taobao.api.request.TbkItemInfoGetRequest;
import com.taobao.api.request.TbkOptimusTouMaterialIdsGetRequest;
import com.taobao.api.response.TbkDgMaterialOptionalUpgradeResponse;
import com.taobao.api.response.TbkDgMaterialRecommendResponse;
import com.taobao.api.response.TbkItemInfoGetResponse;
import com.taobao.api.response.TbkOptimusTouMaterialIdsGetResponse;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

/**
 * 商学院课程Controller
 *
 * @author SystemAuth
 * @date 2023-05-19
 */

@RestController(value = "H5CpsController")
@RequestMapping("/api/cps")
@Slf4j
@Api(tags = "cps推广")
public class CpsController extends BaseController {
    String url = "http://gw.api.taobao.com/router/rest";
    String appkey = "34612608";
    String secret = "a5a82a60540026afc8c472185f9db248";

    @Autowired
    private CpsMapper cpsMapper;
    /**
     * 查询cps-物料列表
     */
    @ApiOperation(value = "列表", response = Cps.class)
    @GetMapping("/wlList")
    public Object wlList() {
        return this.cpsMapper.selectAll();
    }


    @ApiOperation(value = "列表")
    @PostMapping("/list")
    public Object list(@RequestBody PageParam pageParam) throws ApiException {
        TaobaoClient client = new DefaultTaobaoClient(url, appkey, secret);
        TbkDgMaterialRecommendRequest req = new TbkDgMaterialRecommendRequest();
        req.setPageSize(pageParam.getPageSize().longValue());
        req.setPageNo(pageParam.getPage().longValue());
        //官方提供的物料Id
        req.setMaterialId(44413L);
        //	推广位id，
        req.setAdzoneId(115550350011L);
        //渠道关系ID
        req.setRelationId(123456L);
        req.setDeviceType("IMEI");
        req.setDeviceEncrypt("MD5");
        req.setFavoritesId("123445");
        req.setPromotionType("2");
        req.setSpecialId("2323");
        req.setItemId("qeqscd1231-uqwenqe");
        TbkDgMaterialRecommendResponse rsp = client.execute(req);
        return JSONObject.parseObject(rsp.getBody());
    }

    @ApiOperation(value = "物料id列表查询")
    @PostMapping("/getByID")
    public Object getByID(@RequestBody GetByIDVo vo) throws ApiException {
        TaobaoClient client = new DefaultTaobaoClient(url, appkey, secret);
        TbkOptimusTouMaterialIdsGetRequest req = new TbkOptimusTouMaterialIdsGetRequest();
        TbkOptimusTouMaterialIdsGetRequest.MaterialQuery obj1 = new TbkOptimusTouMaterialIdsGetRequest.MaterialQuery();
        obj1.setPageNo(vo.getPage().longValue());
        obj1.setSubject(vo.getSubject());
        obj1.setMaterialType(vo.getMaterialType());
        obj1.setPageSize(vo.getPageSize().longValue());
        req.setMaterialQuery(obj1);
//        String session = "61016068d8ea4c12189775a6d323e836c6916e56daa1ffd1892614477";
        String session = vo.getSessionKey();
//        6102618bb7d449e9b947b373d9138a1736812fea08503b21892614477
        log.info("session------------------,{}",session);
        TbkOptimusTouMaterialIdsGetResponse rsp = client.execute(req,session);
        return JSONObject.parseObject(rsp.getBody());
    }

    @ApiOperation(value = "查询商品详情")
    @GetMapping("/detail/{numIds}")
    public Object detail(@PathVariable("numIds") String numIds) throws ApiException {
        String ip = "";
        if (request.getHeader("x-forwarded-for") == null) {
             ip = request.getRemoteAddr();
        }else {
            ip = request.getHeader("x-forwarded-for");
        }
        TaobaoClient client = new DefaultTaobaoClient(url, appkey, secret);
        TbkItemInfoGetRequest req = new TbkItemInfoGetRequest();
        req.setNumIids(numIds);
//        链接形式：1：PC，2：无线，默认：１
        req.setPlatform(1L);
//        ip地址，影响邮费获取，如果不传或者传入不准确，邮费无法精准提供
        req.setIp(ip);
//        1-动态ID转链场景，2-消费者比价场景，3-商品库导购场景（不填默认为1）
        req.setBizSceneId("1");
//        1-自购省，2-推广赚（代理模式专属ID，代理模式必填，非代理模式不用填写该字段）
        req.setPromotionType("2");
//        渠道关系ID
//        req.setRelationId("1");
//        商品库服务账户(场景id3权限对应的memberid）
//        req.setManageItemPubId(1L);
        TbkItemInfoGetResponse rsp = client.execute(req);
        return JSONObject.parseObject(rsp.getBody());
    }

    @ApiOperation(value = "通用物料搜索API（导购）")
    @PostMapping("/proList")
    public Object proList(@RequestBody PageParam pageParam) throws ApiException {
        TaobaoClient client = new DefaultTaobaoClient(url, appkey, secret);
        TbkDgMaterialOptionalUpgradeRequest req = new TbkDgMaterialOptionalUpgradeRequest();
        req.setPageSize(pageParam.getPageSize().longValue());
        req.setPageNo(pageParam.getPage().longValue());
        req.setSort("tk_rate_des");
//        req.setItemloc("杭州");
        req.setCat("16,18");
        //商品筛选-查询词
//        if(pageParam.getKv().get("q").toString() != null){
//            req.setQ(pageParam.getKv().get("q").toString());
//        }
        Integer materialId = (Integer) pageParam.getKv().get("materialId");
        if (materialId !=null){
            //物料id，不传时默认物料material_id=80309；如果直接对消费者投放，可使用官方个性化算法优化的搜索物料material_id=17004（注意：若物料id=17004没查询到结果则出系统默认物料id=80309的查询结果）
            req.setMaterialId(materialId.longValue());
        }
        req.setHasCoupon(true);
        req.setAdzoneId(115550350011L);
        req.setNpxLevel(2L);
        req.setDeviceEncrypt("MD5");
        req.setDeviceValue("xxx");
        req.setDeviceType("IMEI");
        req.setGetTopnRate(0L);
        req.setBizSceneId("1");
        req.setPromotionType("2");
        req.setMgcStatus("0");
        TbkDgMaterialOptionalUpgradeResponse rsp = client.execute(req);
        return JSONObject.parseObject(rsp.getBody());
    }


}
